package cn.com.infcn.spider.io.output;

import java.util.ArrayList;
import java.util.List;

import org.nutz.lang.Mirror;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.com.infcn.spider.io.ConfigParse;
import cn.com.infcn.spider.io.input.Input;
import cn.com.infcn.spider.io.output.impl.consoleOutput;
import cn.com.infcn.spider.io.output.impl.fileOutput;
import cn.com.infcn.spider.io.output.impl.jdbcOutput;
import cn.com.infcn.spider.io.output.impl.mongoOutput;

public class OutputFactory {

	public static Output[] create(String output, String jobName) {

		List<Output> result = new ArrayList<>();

		JSONArray parse = ConfigParse.parse(output);

		for (Object obj : parse) {
			JSONObject job = (JSONObject) obj;
			String type = job.getString("type");

			try {
				result.add((Output) Mirror.me(Class.forName("cn.com.infcn.spider.io.output.impl." + type + "Output")).born(job.getJSONObject("obj"), jobName));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		}

		return result.toArray(new Output[result.size()]);
	}

}
